package com.sys.basics.controller;

import cn.hutool.core.util.ObjectUtil;
import com.sys.basics.config.annotation.SysLogAnnotation;
import com.sys.basics.entity.SysResource;
import com.sys.basics.entity.SysRole;
import com.sys.basics.entity.SysUser;
import com.sys.basics.service.SysRoleService;
import com.sys.basics.service.SysUserService;
import lombok.extern.slf4j.Slf4j;
import com.sys.basics.config.jsonRes.JsonResult;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.sys.basics.service.SysResourceService;
import io.swagger.annotations.*;

import javax.validation.Valid;
import javax.validation.constraints.*;
import java.util.List;

/**
 * @author: ares
 * @date: 2021-10-08
 * @description: 系统资源表 前端控制器
 */
@Validated
@Slf4j
@Api(tags = "权限资源管理")
@RestController
@RequestMapping("/basics/sysResource")
public class SysResourceController extends AbstractController<SysResourceService, SysResource> {

    private JsonResult<List<SysResource>> listResult = new JsonResult<>();

    @Autowired
    SysResourceService sysResourceService;

    @Autowired
    SysRoleService sysRoleService;

    @Autowired
    SysUserService sysUserService;

    @SysLogAnnotation("根据id查询资源tree")
    @ApiOperation("根据id查询tree")
    @ApiImplicitParam(name = "id", value = "节点id", required = true, defaultValue = "1", paramType = "query")
    @GetMapping("/getTreeById")
    public JsonResult<SysResource> getTreeById(@NotNull(message = "资源id不能为空") Long id) {
        return result.success("tree查询成功", sysResourceService.getTreeById(id));
    }

    @SysLogAnnotation("根据父节点id查询子资源")
    @ApiOperation("根据父节点id查询子资源")
    @ApiImplicitParam(name = "parentId", value = "父节点id", required = true, defaultValue = "0", paramType = "query")
    @GetMapping("/getChildrenByParentId")
    public JsonResult<List<SysResource>> getChildrenByParentId(@NotNull(message = "父资源id不能为空") Long parentId) {
        return listResult.success("资源tree查询成功", sysResourceService.getChildrenByParentId(parentId));
    }

    @SysLogAnnotation("根据父节点id查询资源tree")
    @ApiOperation("根据父节点id查询tree")
    @ApiImplicitParam(name = "parentId", value = "父节点id", required = true, defaultValue = "0", paramType = "query")
    @GetMapping("/getChildrenTreeByParentId")
    public JsonResult<List<SysResource>> getChildrenTreeByParentId(@NotNull(message = "父资源id不能为空") Long parentId) {
        return listResult.success("资源tree查询成功", sysResourceService.getChildrenTreeByParentId(parentId));
    }

    @SysLogAnnotation("查询父资源节点")
    @ApiOperation("查询父节点")
    @GetMapping("/getParentTree")
    public JsonResult<List<SysResource>> getParentTree() {
        return listResult.success("资源tree查询成功", sysResourceService.getParentTree());
    }

    @SysLogAnnotation("根据子节点id查询上级资源tree")
    @ApiOperation("根据子节点id查询上级tree")
    @ApiImplicitParam(name = "id", value = "子节点的id", required = true, defaultValue = "6", paramType = "query")
    @GetMapping("/getTreeByIdAsLeaf")
    public JsonResult<SysResource> getTreeByIdAsLeaf(@NotNull(message = "子资源id不能为空") Long id) {
        return result.success("资源tree查询成功", sysResourceService.getTreeByIdAsLeaf(id));
    }

    @SysLogAnnotation("查询组织拥有的资源tree")
    @ApiOperation("查询组织拥有的资源tree")
    @ApiImplicitParam(name = "orgId", value = "组织id", required = true, defaultValue = "1", paramType = "query")
    @GetMapping("/getTreeByOrgId")
    public JsonResult<List<SysResource>> getTreeByOrgId(@NotNull(message = "组织id不能为空") Long orgId) {
        return listResult.success("组织资源tree查询成功", sysResourceService.getTreeByOrgId(orgId));
    }

    @SysLogAnnotation("查询角色关联的资源")
    @ApiOperation("查询角色关联的资源")
    @ApiImplicitParam(name = "roleId", value = "角色id", required = true, defaultValue = "1", paramType = "query")
    @GetMapping("/getListByRoleId")
    public JsonResult<List<SysResource>> getListByRoleId(@NotNull(message = "角色id不能为空") Long roleId) {
        SysRole role = sysRoleService.getById(roleId);
        if (ObjectUtil.isNull(role)) {
            return listResult.error("角色不存在");
        }

        return listResult.success("查询角色关联的资源成功", sysResourceService.getListByRoleId(roleId));
    }

    @SysLogAnnotation("根据id删除资源自身及子节点")
    @ApiOperation("根据id删除自身及子节点")
    @Override
    public JsonResult removeById(Long id) {
        boolean removed = sysResourceService.removeSelfAndChildrenById(id);
        if (removed) {
            result.success("删除成功");
        } else {
            result.error("删除失败");
        }
        return result;
    }
}
